<template>
  <a-drawer :width="500" :visible="show" :footer="false" @cancel="handleCancel" unmountOnClose>
    <template #title>
      {{data.type}}组件编辑
    </template>
    <div class="setting-content">
      <!-- 组件自身属性 -->
      <div class="setting-header flex items-center">
        <span class="header-title">组件宽度</span>
        <a-select v-model="data.width" placeholder="请选择" allow-clear>
          <a-option value="">默认</a-option>
          <a-option value="small">小</a-option>
          <a-option value="middle">中</a-option>
          <a-option value="max">大</a-option>
        </a-select>
      </div>
      <div class="setting-header flex items-center">
        <span class="header-title">组件背景</span>
        <a-select v-model="data.bgColor" placeholder="请选择" allow-clear>
          <a-option value="#ffffff">白色</a-option>
          <a-option value="#F7F7FA">灰色</a-option>
        </a-select>
      </div>

      <a-button-group type="primary">
        <a-button size="mini" @click="handleAdd('')"><template #icon><icon-plus /></template> 默认按钮 </a-button>
        <a-button size="mini" @click="handleAdd('white')"><template #icon><icon-plus /></template> 白色按钮 </a-button>
        <a-button size="mini" @click="handleAdd('light')"><template #icon><icon-plus /></template> 亮色按钮 </a-button>
        <a-button size="mini" @click="handleAdd('blue')"><template #icon><icon-plus /></template> 蓝色按钮 </a-button>
      </a-button-group>

      <!-- 子组件属性 -->
      <div class="setting-body">
        <div class="setting-item">
          <p class="item-name">按钮</p>
          <draggable
            v-model="data.data"
            :component-data="{
              tag: 'div',
              type: 'transition-group',
              name: !drag ? 'flip-list' : null
            }"
            v-bind="buttonDragOptions"
            :disabled="!shouldShowDragHandle(data.data)"
            @start="drag = true"
            @end="drag = false"
            item-key="id"
          >
            <template #item="{ element: button }">
              <div
                class="item-button draggable-item"
                :class="{ 'sortable-disabled': !shouldShowDragHandle(data.data) }"
                :key="button.id"
              >
                <div class="btn-group">
                  <div
                    v-if="shouldShowDragHandle(data.data)"
                    class="drag-handle"
                  >
                    <icon-drag-arrow class="drag-icon" />
                  </div>
                  <icon-delete class="btn-delete" @click="handleDelete(button.id)" />
                </div>
                <a-button type="primary">{{ button.text }}</a-button>
                <div class="item-action flex items-center">
                  <a-input class="action-text" v-model="button.text" placeholder="按钮文本" allow-clear />
                  <a-input v-model="button.url" placeholder="按钮链接" allow-clear />
                </div>
                <a-checkbox v-model="button.isExternal">是否外部链接</a-checkbox>
              </div>
            </template>
          </draggable>
        </div>
      </div>
    </div>
  </a-drawer>
</template>

<script lang="ts" setup>
import { ref, computed, defineAsyncComponent } from 'vue'
// 动态导入 draggable，避免 SSR 问题
const draggable = defineAsyncComponent(() => import("vuedraggable"));
import { randomString } from '@/utils/common'

// 拖拽相关状态
const drag = ref(false)

// 不同类型的拖拽配置选项
const buttonDragOptions = computed(() => ({
  animation: 200,
  group: 'buttons', // 按钮专用分组
  disabled: false,
  ghostClass: 'ghost',
  handle: ".drag-handle", // 只有拖拽手柄才能触发拖拽
  filter: ".arco-input, .arco-select, .arco-checkbox", // 排除输入框等元素
  preventOnFilter: false, // 允许在被过滤的元素上进行正常交互
}))

// 检查是否应该显示拖拽图标
const shouldShowDragHandle = (list: any[]) => {
  return list && list.length > 1
}

const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },

  data: {
    type: Object,
    default: () => {}
  }
})

const emit = defineEmits(['update:show']);

const handleCancel = () => {
  emit('update:show', false);
}

const handleAdd = (type: string) => {
  const detail = props.data;
  detail.data.push({
    id: randomString(),
    type,
    text: '查看更多',
    url: undefined,
    isExternal: false
  })
}

const handleDelete = (id: string) => {
  props.data.data = props.data.data.filter((item: any) => item.id !== id)
}
</script>

<style lang="scss" scoped>
.setting-content {
  .setting-header {
    padding-bottom: 12px;

    .header-title {
      width: 120px;
      padding-right: 12px;
      text-align: right;
    }
  }

  .setting-body {
    margin-top: 12px;
    
    .setting-item {
      position: relative;
      padding: 16px 12px;
      background: #f0f2f5;
      border-radius: 8px;
      margin-bottom: 20px;

      .btn-group {
        position: absolute;
        right: 0;
        top: 0;
        display: flex;
        align-items: center;
        padding: 4px;

        .btn-delete {
          padding: 4px;
          font-size: 24px;
          cursor: pointer;
        }
      }

      .item-name {
        font-size: 16px;
        font-weight: 600;
        padding-bottom: 10px;
        border-bottom: 1px solid #fff;
      }

      .item-button {
        position: relative;
        padding: 12px 8px 4px;
        background: #fff;
        border-radius: 4px;
        margin: 4px 0;
        transition: all 0.3s ease;

        &.draggable-item {          &:hover {
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            transform: translateY(-1px);
          }

          // 当禁用拖拽时，不显示拖拽效果
          &.sortable-disabled {
            cursor: default;

            &:hover {
              box-shadow: none;
              transform: none;
            }
          }
        }

        .arco-input-wrapper {
          background: #f2f3f5;
        }
      }

      // 拖拽相关样式
      .flip-list-move {
        transition: transform 0.5s;
      }

      .no-move {
        transition: transform 0s;
      }

      .ghost {
        opacity: 0.5;
        background: #c8ebfb;
      }

      .item-action {
        padding: 10px 0;

        .action-text {
          width: 150px;
          margin-right: 8px;
        }
      }
    }
  }

  .drag-handle {
    padding: 4px;
    cursor: grab;
    padding: 4px;
    border-radius: 4px;

    &:hover {
      background-color: #f0f2f5;
    }

    &:active {
      cursor: grabbing;
    }

    .drag-icon {
      font-size: 16px;
      color: #86909c;
    }
  }
}
</style>